using System;
using System.Collections.Generic;
using System.Linq;
using Toon.Models.Catalogs;

namespace Toon.Models.Characters
{
	public abstract class RuleSet
	{
		/// <summary>
		/// Initializes a new instance of the RuleSet class.
		/// </summary>
		/// <param name="catalog"></param>
		protected RuleSet(Catalog catalog)
		{
			m_Catalog = catalog;
		}

		readonly Catalog m_Catalog;

		public Catalog Catalog
		{
			get { return m_Catalog; }
		}

		public virtual int MaxSkillPoints
		{
			get { return 30; }
		}

        public string Name
        {
            get { return "Standard"; }
        }

        private const int MaxGizmos = 1;
		private const int MaxUniquePossessions = 4;
		private const int MaxPossessions = 8;

		public void AddRandomPossession(Character character)
		{
			PossessionDefinition newPossession = null;
			var knownItems = character.Possessions.Select(p => p.Name).ToList();
			while (newPossession == null)
			{
				var newItem = Catalog.Possessions.ChooseOne(character.Random);
				var gizmoCount = character.Possessions.Where(p => p.Type == PossessionType.Gizmo).Count();
				var unusualCount = gizmoCount + character.Possessions.Where(p => p.Type == PossessionType.Unusual).Count();
				if (!knownItems.Contains(newItem.Name))
				{
					if (newItem.Type == PossessionType.Normal)
						newPossession = newItem;
					else if (newItem.Type == PossessionType.Unusual && unusualCount < MaxUniquePossessions)
						newPossession = newItem;
					else if (newItem.Type == PossessionType.Gizmo && gizmoCount < MaxGizmos)
						newPossession = newItem;
				}
			}
			character.Possessions.Add(newPossession);
		}

		public void AddRandomPossessions(Character character)
		{
			while (character.Possessions.Count < MaxPossessions)
				AddRandomPossession(character);
		}


		public abstract Character CreateNewCharacter();
		public abstract Character CreateRandomCharacter();
	}
}
